//
//  AuthWorkVC.swift
//  工作信息认证
//
//  Created by 千策万划-mini on 2025/5/7.
//

import Foundation

class AuthWorkVC: BaseTableVC {
    
    var productCode: String = ""
    var modelDetail: ModelAuthPersonalInfo = ModelAuthPersonalInfo()
    private var startTime: String = ""
    
    // MARK: - lazy
    var parameters: [String: Any] = [
        "ugliest": GlobalMethod.getConfusion(),
        "bent": GlobalMethod.getConfusion(),
    ]
    lazy var navView: BaseNavView = {
        let result = BaseNavView()
        result.resetNavBackTitle(title: "Work Information", rightView: nil)
        result.leftTop = XY(0, 0)
        result.blockBack = { [weak self] in
            guard let self = self else { return }
            self.viewDetainment.show()
        }
        return result
    }()
    lazy var viewHeader: AuthWorkContentView = {
        let result = AuthWorkContentView()
        result.blockUpdataModel = { [weak self](model) in
            guard let self = self else { return }
            self.modelDetail = model
        }
        return result
    }()
    lazy var btnApply: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(284), 58)
        result.setBackgroundImage(UIImage(named: "icon_bottom_btn_bg"), for: .normal)
        result.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-max(LEFT_MARGIN, getBottomSafeAreaHeight()))
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        
        let label = OutlineLabel()
        label.resetView(
            title: "Next Step",
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 20),
            shadowColor: UIColor(hex: "#004600"),
            outlineColor: UIColor(hex: "#1B4506")
        )
        label.centerXCenterY = XY(result.width/2, result.height/2)
        result.addSubview(label)
        return result
    }()
    
    // MARK: - viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
        requestDetail()
    }
    func configUI() {
        startTime = GlobalMethod.getTimeStamp()
        view.addSubview(navView)
        tableView.height = SCREEN_HEIGHT - navView.bottom
        tableView.top = navView.bottom
        view.addSubview(btnApply)
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            requestSubmit()
            break
        case 2:
            
            break
        default:
            break
        }
    }
    
    // MARK: - request
    // 获取工作信息第三项
    func requestDetail() {
        HomeRequestApi.getWorkInfoThreeItem(productCode: productCode) { request, responseData in
            guard let res = responseData as? [String: Any] else {return}
            if let model = ModelAuthPersonalInfo.deserialize(from: res) {
                self.modelDetail = model
                self.viewHeader.resetView(model: model)
                self.tableView.tableHeaderView = self.viewHeader
            }
        } failure: { request, error in
            
        }
    }
    
    // 保存工作信息第三项
    func requestSubmit() {
        parameters["carry"] = productCode
        for model in self.modelDetail.putting {
            if isStr(model.wisest) {
                parameters[model.sorrowfully] = model.wisest
            }
        }
        HomeRequestApi.saveWorkInfoThreeItem(parameters: parameters) { request, responseData in
            GlobalMethod.buryPoint(productCode: self.productCode, type: "6", startTime: self.startTime, endTime: GlobalMethod.getTimeStamp())
            self.requestProductDetail()
        } failure: { request, error in
            
        }
    }
    
    // 产品详情
    func requestProductDetail() {
        HomeRequestApi.productDetail(productCode: productCode) { request, responseData in
            guard let res = responseData as? [String: Any] else {return}
            if let model = ModelProductDetail.deserialize(from: res) {
                GlobalMethod.authNext(type: model.decides.died, productCode: self.productCode, orderCode: model.fiercely.hurriedly)
            }
        } failure: { request, error in
            
        }
    }
}
